package com.cnbbx.mybatis.definition;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Data
public class EntityDefinition {

    private String id;
    private String table;
    private String tableAs;
    private JoinType joinType;
    private String joinTable;
    private String joinTableAs;
    private String tableOn;
    private String joinTableOn;
    private String moreTable;
    private String moreOn;
    private Map<String, EntityColumnDefinition> columnDefinitions = new HashMap<String, EntityColumnDefinition>();

    /**
     * 表头定义
     *
     * @return
     */
    public String table() {
        if (StringUtils.isBlank(tableAs)) {
            return table;
        }
        return table + " as " + tableAs;
    }

    /**
     * 加入子表
     *
     * @return
     */
    public String joinTable() {
        if (StringUtils.isBlank(joinTable)) {
            throw new RuntimeException("join table is null（joinTable）");
        }
        return joinTable + " as " + joinTableAs;
    }

    /**
     * 主表和子表关系
     *
     * @return
     */
    public String on() {
        if (StringUtils.isBlank(tableOn) || StringUtils.isBlank(joinTableOn)) {
            throw new RuntimeException("on must not null（tableOn，joinTableOn）");
        }
        return tableAs + "." + tableOn + "=" + joinTableAs + "." + joinTableOn;
    }

    /**
     * 注册字段属性
     *
     * @param p
     * @param entityColumnDefinition
     */
    public void addEntityColumnDefinition(String p, EntityColumnDefinition entityColumnDefinition) {
        columnDefinitions.put(p, entityColumnDefinition);
    }

}
